import { ref } from "vue";
import moment from "moment/moment";
import { timeSlot } from "@/utils";
import { getVppListAll } from "@/api/vppUser";
import {
  getVppMonitor,
  getVppUserMonitor
} from "@/api/SchedulingPlanMonitoring";

export function setDataHook() {
  const selDay = ref(moment().format("MM月DD日"));
  const selDayValue = ref(moment().format("YYYY-MM-DD"));

  const showType = ref("chart");
  const showType2 = ref("chart");
  // function getRandomNumberInRange(start, end) {
  //   const random = Math.random() * (end - start + 1) + start;
  //   return parseFloat(random.toFixed(2));
  // }

  const data = ref([]);
  const data2 = ref([]);
  const times = timeSlot(15);
  const keys = Array(96)
    .fill(1)
    .map((_, index) => index + 1)
    .map(i => `p${i > 9 ? i : "0" + i}`);
  // times.forEach(item => {
  //   let c = [
  //     item,
  //     getRandomNumberInRange(4.9, 5), // 最大值
  //     getRandomNumberInRange(3.5, 4.8), // 实际值
  //     getRandomNumberInRange(3, 3.5), // 最小值
  //     getRandomNumberInRange(0, 6),
  //     getRandomNumberInRange(0, 6),
  //     getRandomNumberInRange(0, 6)
  //   ];
  //   data.value.push(c);
  //   data2.value.push(c);
  // });
  const options = ref({
    color: [
      "#fac858",
      "#ee6666",
      "#fc8452",
      "#91cc75",
      "#3ba272",
      "#9a60b4",
      "#ea7ccc"
    ],
    grid: {
      top: "10%",
      left: "4%",
      right: "4%",
      bottom: "20%"
    },
    legend: {
      show: true,
      top: "0%",
      data: ["基线", "实际", "实际调节", "计划调节", "调节时段"]
    },
    dataZoom: [
      {
        type: "inside",
        start: 0,
        end: 100
      },
      {
        show: true,
        type: "slider",
        top: "90%",
        start: 0,
        end: 100
      }
    ],
    tooltip: {
      trigger: "axis"
    },
    xAxis: {
      splitLine: {
        show: false
      },
      data: times
    },
    yAxis: [
      {
        name: "kW",
        nameLocation: "end",
        splitLine: {
          show: false
        },
        axisLine: {
          show: true
        }
      }
    ],
    series: [
      {
        name: "基线",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["baseLineData"];
        })
      },
      {
        name: "实际",
        type: "line",
        smooth: true,
        data: data.value.map(item => {
          return item["realData"];
        })
      },
      {
        name: "实际调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["realRegulate"];
        })
      },
      {
        name: "计划调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["planRegulate"];
        })
      },
      {
        name: "调节时段",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["regulateTime"];
        })
      }
    ]
  });
  const options2 = ref({
    color: [
      "#fac858",
      "#ee6666",
      "#fc8452",
      "#91cc75",
      "#3ba272",
      "#9a60b4",
      "#ea7ccc"
    ],
    grid: {
      top: "10%",
      left: "4%",
      right: "4%",
      bottom: "20%"
    },
    legend: {
      show: true,
      top: "0%",
      data: ["基线", "实际", "实际调节", "计划调节", "调节时段"]
    },
    dataZoom: [
      {
        type: "inside",
        start: 0,
        end: 100
      },
      {
        show: true,
        type: "slider",
        top: "90%",
        start: 0,
        end: 100
      }
    ],
    tooltip: {
      trigger: "axis"
    },
    xAxis: {
      splitLine: {
        show: false
      },
      data: times
    },
    yAxis: [
      {
        name: "kW",
        nameLocation: "end",
        splitLine: {
          show: false
        },
        axisLine: {
          show: true
        }
      }
    ],
    series: [
      {
        name: "基线",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["baseLineData"];
        })
      },
      {
        name: "实际",
        type: "line",
        smooth: true,
        data: data.value.map(item => {
          return item["realData"];
        })
      },
      {
        name: "实际调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["realRegulate"];
        })
      },
      {
        name: "计划调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["planRegulate"];
        })
      },
      {
        name: "调节时段",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: data.value.map(item => {
          return item["regulateTime"];
        })
      }
    ]
  });

  const loading = ref(false);
  const userData = ref([]);
  const vppList = ref([]);
  const activeVppId = ref(undefined);
  const activeData = ref(null);
  const activeUserData = ref(null);

  const rightData = ref({
    // 计划调节量
    planRegulateAmount: undefined,
    // 实际调节量
    realRegulateAmount: undefined,
    // 调节偏差
    regulateDeviation: undefined
  });
  const getVppUserMonitorData = async () => {
    let copyObj = {};
    keys.forEach(key => {
      copyObj[key] = undefined;
    });
    let dataAll = await getVppUserMonitor({
      dataType: 2,
      date: selDayValue.value,
      // vppId: activeData.value.id,
      vppUserId: activeUserData.value.id
    });
    if (dataAll) {
      // 基线
      let baseLineData =
        dataAll && dataAll.baseLine
          ? dataAll.baseLine
          : JSON.parse(JSON.stringify(copyObj));
      // 实际
      let realData =
        dataAll && dataAll.realData
          ? dataAll.realData
          : JSON.parse(JSON.stringify(copyObj));
      // 实际调节
      let realRegulate =
        dataAll && dataAll.realRegulate
          ? dataAll.realRegulate
          : JSON.parse(JSON.stringify(copyObj));
      // 计划调节
      let planRegulate =
        dataAll && dataAll.planRegulate
          ? dataAll.planRegulate
          : JSON.parse(JSON.stringify(copyObj));
      // 调节时段
      let regulateTime =
        dataAll && dataAll.regulateTime
          ? dataAll.regulateTime
          : JSON.parse(JSON.stringify(copyObj));
      let topDataOne = [];
      keys.forEach((key, index) => {
        let item = {
          time: times[index],
          baseLineData: baseLineData[key] || undefined,
          realData: realData[key] || undefined,
          realRegulate: realRegulate[key] || undefined,
          planRegulate: planRegulate[key] || undefined,
          regulateTime: regulateTime[key] || undefined
        };
        topDataOne.push(item);
      });
      data2.value = topDataOne;
      options2.value.series = [
        {
          name: "基线",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data2.value.map(item => {
            return item["baseLineData"];
          })
        },
        {
          name: "实际",
          type: "line",
          smooth: true,
          data: data2.value.map(item => {
            return item["realData"];
          })
        },
        {
          name: "实际调节",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data2.value.map(item => {
            return item["realRegulate"];
          })
        },
        {
          name: "计划调节",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data2.value.map(item => {
            return item["planRegulate"];
          })
        },
        {
          name: "调节时段",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data2.value.map(item => {
            return item["regulateTime"];
          })
        }
      ];
    }
  };
  const getVppMonitorData = async () => {
    let copyObj = {};
    keys.forEach(key => {
      copyObj[key] = undefined;
    });
    activeUserData.value = null;
    data.value = [];
    data2.value = [];
    rightData.value = {
      // 计划调节量
      planRegulateAmount: undefined,
      // 实际调节量
      realRegulateAmount: undefined,
      // 调节偏差
      regulateDeviation: undefined
    };
    options.value.series = [
      {
        name: "基线",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      },
      {
        name: "实际",
        type: "line",
        smooth: true,
        data: []
      },
      {
        name: "实际调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      },
      {
        name: "计划调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      },
      {
        name: "调节时段",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      }
    ];
    options2.value.series = [
      {
        name: "基线",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      },
      {
        name: "实际",
        type: "line",
        smooth: true,
        data: []
      },
      {
        name: "实际调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      },
      {
        name: "计划调节",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      },
      {
        name: "调节时段",
        type: "line",
        smooth: true,
        // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
        data: []
      }
    ];
    let dataAll = await getVppMonitor({
      dataType: 1,
      date: selDayValue.value,
      vppId: activeData.value.id
    });
    if (dataAll) {
      userData.value = dataAll && dataAll.userInfos ? dataAll.userInfos : [];
      activeUserData.value = userData.value.length ? userData.value[0] : null;
      if (activeUserData.value) {
        await getVppUserMonitorData();
      }
      // 基线
      let baseLineData =
        dataAll && dataAll.baseLine
          ? dataAll.baseLine
          : JSON.parse(JSON.stringify(copyObj));
      // 实际
      let realData =
        dataAll && dataAll.realData
          ? dataAll.realData
          : JSON.parse(JSON.stringify(copyObj));
      // 实际调节
      let realRegulate =
        dataAll && dataAll.realRegulate
          ? dataAll.realRegulate
          : JSON.parse(JSON.stringify(copyObj));
      // 计划调节
      let planRegulate =
        dataAll && dataAll.planRegulate
          ? dataAll.planRegulate
          : JSON.parse(JSON.stringify(copyObj));
      // 调节时段
      let regulateTime =
        dataAll && dataAll.regulateTime
          ? dataAll.regulateTime
          : JSON.parse(JSON.stringify(copyObj));
      let topDataOne = [];
      keys.forEach((key, index) => {
        let item = {
          time: times[index],
          baseLineData: baseLineData[key] || undefined,
          realData: realData[key] || undefined,
          realRegulate: realRegulate[key] || undefined,
          planRegulate: planRegulate[key] || undefined,
          regulateTime: regulateTime[key] || undefined
        };
        topDataOne.push(item);
      });
      data.value = topDataOne;
      options.value.series = [
        {
          name: "基线",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data.value.map(item => {
            return item["baseLineData"];
          })
        },
        {
          name: "实际",
          type: "line",
          smooth: true,
          data: data.value.map(item => {
            return item["realData"];
          })
        },
        {
          name: "实际调节",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data.value.map(item => {
            return item["realRegulate"];
          })
        },
        {
          name: "计划调节",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data.value.map(item => {
            return item["planRegulate"];
          })
        },
        {
          name: "调节时段",
          type: "line",
          smooth: true,
          // 最小值+1  最小值 - 1 最小值 - 1  最小值+1
          data: data.value.map(item => {
            return item["regulateTime"];
          })
        }
      ];
      rightData.value.planRegulateAmount =
        dataAll.planRegulateAmount === 0
          ? 0
          : dataAll.planRegulateAmount || undefined;
      rightData.value.realRegulateAmount =
        dataAll.realRegulateAmount === 0
          ? 0
          : dataAll.realRegulateAmount || undefined;
      rightData.value.regulateDeviation =
        dataAll.regulateDeviation === 0
          ? 0
          : dataAll.regulateDeviation || undefined;
    }
  };
  getVppListAll().then(async res => {
    loading.value = true;
    vppList.value = res || [];
    activeData.value = res.length ? res[0] : null;
    await getVppMonitorData();
    loading.value = false;
  });
  const command = async e => {
    activeData.value = vppList.value.find(item => item.id === e);
    await getVppMonitorData();
  };
  const rowClick = async row => {
    // console.log(row);
    if (row.id === activeUserData.value.id) return;
    loading.value = true;
    activeUserData.value = row;
    if (activeUserData.value) {
      await getVppUserMonitorData();
    }
    loading.value = false;
  };
  const selDayChange = async v => {
    let z = moment(selDay.value, "MM月DD日").add(v, "days");
    selDay.value = z.format("MM月DD日");
    selDayValue.value = z.format("YYYY-MM-DD");
    await getVppMonitorData();
  };
  return {
    loading,
    selDay,
    selDayChange,
    showType,
    showType2,
    data,
    data2,
    options,
    options2,
    vppList,
    activeVppId,
    activeData,
    command,
    userData,
    rowClick,
    rightData
  };
}
